package com.controller;


import com.config.Result;
import com.dao.EmployeeDao;
import com.pojo.Building;
import com.pojo.DutyRecord;
import com.pojo.Employee;
import com.pojo.User;
import com.service.impl.BuildServiceImpl;
import com.service.impl.DutyRecordServiceImpl;
import com.service.impl.EmployeeServiceImpl;
import com.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ZhiBan")
public class ZhiBanController {

    @Autowired
    DutyRecordServiceImpl dutyRecordService;

    @Autowired
    EmployeeDao employeeDao;

    @Autowired
    BuildServiceImpl buildService;

    @GetMapping("/searchAll")
    public Result<?> searchAll(@RequestParam String param){
        List<DutyRecord> dutyRecords = dutyRecordService.selectAll(param);
        if (dutyRecords == null) {
            return Result.error("-1", "查看失败");
        }
        return Result.success(dutyRecords);
    }

    @DeleteMapping("/deleteRecord/{username}")
    public Result<?> deleteRecord(@PathVariable String username){
        if (username == null){
            return Result.error("-1","账号为空");
        }
        Integer integer = dutyRecordService.deleteByUsername(username);
        if (integer != 1){
            return Result.error("-1","删除失败");
        }
        return Result.success();
    }

    @PostMapping("/addRecord")
    public Result<?> addRecord(@RequestBody DutyRecord dutyRecord){
        if (dutyRecord == null){
            return Result.error("-1","数据为空");
        }
        String username = dutyRecord.getUsername();
        User user = new User();
        user.setUsername(username);

        Employee employee = employeeDao.FindEmployeeByUsername(username, "2");

        if (employee == null){
            return Result.error("-1","不存在该管理员");
        }

        String buildingName = dutyRecord.getBuildingName();
        Building building = buildService.selectByName(buildingName);
        if (building == null){
            return Result.error("-1","不存在该宿舍楼");
        }

        Integer integer = dutyRecordService.addRecord(dutyRecord, Integer.parseInt(building.getId()));
        if (integer != 1){
            return Result.error("-1","添加失败");
        }
        return Result.success();
    }

}
